//
// Created by moon on 2023/9/19.
//
#include "iostream"
#include "stm32Update.h"

#define FIRMWARE_MAX_SIZE   (190 * 0x400)

#define BOOT_INFO_ADDR      (FLASH_BASE + 0x20000)  // boot info store addr
#define FIRMWARE_APP0_ADDR  (FLASH_BASE + 0x21000)  // app0 addr 0x8021000
#define FIRMWARE_APP1_ADDR  (FIRMWARE_APP0_ADDR + FIRMWARE_MAX_SIZE)    // app1 addr 0x8050800

Stm32Update ymodem(BOOT_INFO_ADDR, FIRMWARE_MAX_SIZE);

static void update_finish(YModem::Status status) {

    if (status == YModem::StatusFinish) {
        /** file transfer successful */
        Stm32Update::FileProperty_t *info = ymodem.fileGetProperty();

        Serial.println("file transfer successful");
        Serial.printf("file name: %s\n", info->name);
        Serial.printf("file size: %d\n", info->size);
        ymodem.successful();
    } else {
        /** file transfer failed */
        Serial.println("file transfer failed");
        ymodem.fail();
    }

    /** system reset */
    NVIC_SystemReset();
}

void main() {
//    printf("bootloader.\r\n");

    while (true) {
        /* receive file */
        ymodem.receive();

       if (ymodem.isOver()) {
        /** file transfer over.
         * do user code */
        update_finish(ymodem.getStatus());
        }
    }

}
